<?php

/**
 * 默认控制器
 */
class Controller_Album extends Controller_Abstract
{
	function actionIndex()
	{
		$user = $this->_app->CurrentUser();
		if(!empty($user)){
			$uid = $this->_context->get('uid', Util::infoEncryption($user['id']));
			$uid = Util::infoDecryption($uid);
			$albumModel = new Album();
			$albums = $albumModel->find('*', array('uid' => $uid));
			
			$currAlbumId = $this->_context->get('aid', Util::infoEncryption($albums[0]['id']));
			$currAlbumId = Util::infoDecryption($currAlbumId);
			$albumModel->attributes['id'] = $currAlbumId;
			if($albumModel->checkAlbumOwn($uid)){
				$albums = $albumModel->setColumnAsKey($albums, 'id');
				$albums[$currAlbumId]['active'] = true;
				$photoModel = new Photo();
				$photos = $photoModel->find('*', array('uid' => $uid, 'albumid' => $currAlbumId));
				
				$this->_view['albums'] = $albums;
				$this->_view['photos'] = $photos;
				$this->_view['albumId'] = $currAlbumId;
			}
		}
	}
	
	function actionUploadphotolocal()
	{
		$user = $this->_app->CurrentUser();
		$albumModel = new Album();
		$albums = $albumModel->find('*', array('uid' => $user['id']));
		
		$this->_view['albums'] = $albums;
		$this->_view['sessid'] = Util::infoEncryption(session_id());		
	}
	
	function actionUploadphotograb()
	{
		$user = $this->_app->CurrentUser();
		$albumModel = new Album();
		$albums = $albumModel->find('*', array('uid' => $user['id']));
		
		$this->_view['albums'] = $albums;
	}
	
	function actionGetphotos()
	{
		$user = $this->_app->CurrentUser();
		$albumId = $this->_context->get('albumId', null);
		$albumModel = new Album();
		$albumModel->attributes['id'] = Util::infoDecryption($albumId);
		if($albumModel->checkAlbumOwn($user['id'])){
			$albumId = Util::infoDecryption($albumId);
			$photoModel = new Photo();
			$photos = $photoModel->find('*', array('albumid' => $albumId));
			if(empty($photos)){
				$photos = false;
			}
			$this->_returnAjax($photos);
		}else{
			$this->_returnAjax(false);
		}
	}
	
	function actionProcessuploadify()
	{
		$sessionId = Util::infoDecryption($_POST['sessid']);
		session_id($sessionId);
		session_start();
		
		if(empty($_POST['albumId'])){
			$this->_returnAjax(array(
				'result' => false, 
				'error' => Util::getMessage('NO-ALBUM-SELECTED')
			));
		}
		$user = $this->_app->CurrentUser();
		if(empty($user)){
			$this->_returnAjax(array(
				'result' => false, 
				'error' => Util::getMessage('NOT-LOGIN')
			));
		}
		$quality = intval($this->_context->post('quality', PHOTO_DEFAULT_QUALITY));
		if($quality < 30 || $quality > 99){
			$quality = PHOTO_DEFAULT_QUALITY;
		}
		
		$nowDate = date('Y-m-d H:i:s');
		$albumId = Util::infoDecryption($_POST['albumId']);
		
		$albumModel = new Album();
		$albumModel->attributes['id'] = $albumId;
		if($albumModel->checkAlbumOwn($user['id'])){
			$uploadResult = Util::processUploadifyPhoto($user, $albumId, $quality);
			if($uploadResult['result']){
				$photoModel = new Photo();
				$photoModel->attributes = array(
					'albumid' => $albumId,
					'uid' => $user['id'],
					'`create`' => $nowDate,
					'size' => $uploadResult['orignalFileSize'],
					'quality' => $quality,
					'hasThumbnail' => 0,
					'type' => 'local'
				);
				if(is_readable($uploadResult['orignalFile'])){
					$photoModel->attributes['filename'] = $uploadResult['fileName'];
					if(is_readable($uploadResult['thumbnailFile'])){
						$photoModel->attributes['hasThumbnail'] = 1;
					}
					$photoModel->save();
					$this->_returnAjax(array(
						'result' => true,
						'fileName' => $uploadResult['fileName']
					));
				}
				
				$this->_returnAjax(array(
					'result' => false, 
					'error' => Util::getMessage('UNKNOWN-ERROR')
				));
			}else{
				$this->_returnAjax($uploadResult);
			}
		}else{
			$this->_returnAjax(array(
				'result' => false, 
				'error' => Util::getMessage('NO-ALBUM-SELECTED')
			));
		}
	}
	
    public function actionGraburlphoto()
    {
    	if($this->_context->isGet()){
    		$grabContent = array();
    		$url = $this->_context->get('urltext', '');
    	    if(strpos($url, 'https://') !== 0){
	    		$url = 'http://' . str_replace('http://', '', $url);
	    	}
	    	
	    	$urlParse = Util::parseUrl($url);
    		$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			if($urlParse['scheme'] == 'https'){
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			}
			curl_setopt($ch, CURLOPT_POST, 0);
			$content = curl_exec($ch);
			curl_close($ch);
	    	
			phpQuery::newDocumentHTML($content);
	    	/*
			phpQuery::newDocumentFile($url);
			*/
			
	    	foreach(pq('img') as $img){
	    		$grabContent[] = pq($img)->attr('src');
	    	}
			phpQuery::$documents = array();			//clean data in memory
			
	    	if(empty($grabContent)){
	    		$grabContent = false;
	    	}
			$this->_returnAjax($grabContent);
    	}
    }
    
    public function actionSavegrabphotos()
    {
    	if(empty($_POST['albumId'])){
			$this->_returnAjax(array(
				'result' => false, 
				'error' => Util::getMessage('NO-ALBUM-SELECTED')
			));
		}
		$user = $this->_app->CurrentUser();
		if(empty($user)){
			$this->_returnAjax(array(
				'result' => false, 
				'error' => Util::getMessage('NOT-LOGIN')
			));
		}
		$quality = intval($this->_context->post('quality', PHOTO_DEFAULT_QUALITY));
		if($quality < 30 || $quality > 99){
			$quality = PHOTO_DEFAULT_QUALITY;
		}
		
		$saveMode = $this->_context->post('saveMode', 'save_onlylink');
		
		$nowDate = date('Y-m-d H:i:s');
		$albumId = Util::infoDecryption($_POST['albumId']);
		
		$albumModel = new Album();
		$albumModel->attributes['id'] = $albumId;
		if($albumModel->checkAlbumOwn($user['id'])){
			if($saveMode == 'save_real'){
				$uploadResult = Util::processGrabPhoto($user, $albumId, $quality);
				if($uploadResult['result']){
					$ret = array('result' => true, 'detail' => array());
					if(!empty($uploadResult['detail'])){
						$photoModel = new Photo();
						foreach($uploadResult['detail'] as $k => $detail){
							$oneLine = array(
								'albumid' => $albumId,
								'uid' => $user['id'],
								'`create`' => $nowDate,
								'size' => $detail['orignalFileSize'],
								'quality' => $quality,
								'hasThumbnail' => 0,
								'type' => 'local'
							);
							if(is_readable($detail['orignalFile'])){
								$oneLine['filename'] = $detail['fileName'];
								if(is_readable($detail['thumbnailFile'])){
									$oneLine['hasThumbnail'] = 1;
								}
								$photoModel->attributesArray[] = $oneLine;
								$ret['detail'][$k]['fileName'] = $detail['fileName'];
								$ret['detail'][$k]['mark'] = $detail['mark'];
							}
						}
						if(!$photoModel->saveArray()){
							Util::deleteSavedPhotos($ret['detail']);
							$this->_returnAjax(array(
								'result' => false, 
								'error' => Util::getMessage('SAVE-GRAB-PHOTO-ERROR')
							));
						}
					}
					
					$this->_returnAjax($ret);
				}else{
					$this->_returnAjax($uploadResult);
				}
			}else{
				$imgQueue = $_POST['imgQueue'];
				if(!empty($imgQueue)){
					$ret = array('result' => true, 'detail' => array());
					
					$photoModel = new Photo();
					foreach($imgQueue as $k => $img){
						$oneLine = array(
							'filename' => $img['src'],
							'albumid' => $albumId,
							'uid' => $user['id'],
							'`create`' => $nowDate,
							'size' => 0,
							'quality' => 0,
							'hasThumbnail' => 0,
							'type' => 'grabed'
						);
						$photoModel->attributesArray[] = $oneLine;
						$ret['detail'][$k]['fileName'] = $img['src'];
						$ret['detail'][$k]['mark'] = $img['mark'];
					}
					if(!$photoModel->saveArray()){
						$this->_returnAjax(array(
							'result' => false, 
							'error' => Util::getMessage('SAVE-GRAB-PHOTO-ERROR')
						));
					}
					$this->_returnAjax($ret);
				}
			}
		}else{
			$this->_returnAjax(array(
				'result' => false, 
				'error' => Util::getMessage('NO-ALBUM-SELECTED')
			));
		}
    }
    
    
    
}


